﻿using System;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;
using Telerik.Web.Mvc;

namespace SapaFinance.UI.Controllers
{
    public class BudgetItemController : Controller
    {
        private readonly IBudgetItemService _budgetItemService;

        public BudgetItemController(IBudgetItemService budgetItemService)
        {
            _budgetItemService = budgetItemService;
        }

        public virtual ActionResult Index()
        {
            return View("Index",_budgetItemService.GetListDTO());
        }

        public virtual ActionResult RenderBudgetItem(Guid id)
        {
            return View("EditorTemplates/BudgetItemList", _budgetItemService.GetSelectedListById(id));
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(BudgetItemDTO dto)
        {
            if (ModelState.IsValid){
                _budgetItemService.Insert(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Delete(Guid id)
        {
            _budgetItemService.Delete(id);
            return this.RedirectToAction(x => x.Index());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(BudgetItemDTO dto)
        {
            if (ModelState.IsValid){
                _budgetItemService.Update(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }
    }
}